<?

function exception_handler( Exception $e )
{
	if ( $e instanceof notfound_exception )
	{
		context::set('notfound_exception', $e);
		return front_controller::dispatch_action( 'error_notfound_action' );
	}

	$output['exception'] = true;
	$output['title'] = _('Oops! Something strange is happening');
	if ( $e instanceof pub_exception ) $output['title'] = $e->getMessage();

	$output['message'] = config::get('exceptions_text');

	if ( config::get('debug') ) $output['message'] .= '<pre>' . $e . '</pre>';

	header('Content-type: text/html; charset=utf-8');
	if ( $_SERVER['REQUEST_URI'] )
		echo "<div style='width: 500px; margin: 25px auto; font-family: verdana; text-align: center;'>
				<h1 style='color: darkred;' class=\"exception\">{$output['title']}</h1>
				<a href='javascript:;' onclick='history.go(-1);'>{$output['message']}</a>
			</div>";
	else
		echo $e;

	error_log($e);
}

set_exception_handler('exception_handler');

if ( config::get('debug_warnings') ) set_error_handler ('error_handler');

function error_handler($no, $error, $file, $line, $context)
{
	if ( $no == E_NOTICE ) return;

	echo "<h1>{$error}</h1>";
	echo "<p>{$line}: {$file}</p>";
	foreach ( debug_backtrace() as $trace )
		echo '<li>' . $trace['line'] . ': ' . $trace['file'];
}